<?php
namespace Home\Controller;
use Think\Controller;

/**
* 参数  控制器
*/
class DatapoolController extends HomeBasicController{
	
	public $Datasheet = '';
	public $Datafield = '';
	public $Function ='';
	public $Parameter = '';

	public function _initialize(){
		parent::_initialize();
		$this->checkLogin();
		$this->Datasheet = D('Datasheet');
		$this->Datafield = D('Datafield');
		//$this->Function = D('Function');
		//$this->Parameter = D('Parameter');
		$active = $_GET['f_id'];
		$this->assign('active',$active);
	}

	/**
	 * 显示数据表名称信息
	 * 传参方式 get
	 * @author mss
	 * @time 2017-08-02
	 * @param
	 */
	public function index(){
		//查询所有数据表名称
		$data_list = $this->Datasheet->selectDatasheet('','','');

		//查询第一张表的id
		$id = M('datasheet')->order('d_id ASC')->getField('d_id');
		//查询当前数据表的字段信息
		$where['d_id'] = $_GET['d_id']?$_GET['d_id']:$id;
		$datafields = $this->Datafield->selectDatafield($where,'','');
		//查询当前表信息
		$datainfo = $this->Datasheet->findDatasheet($where);

		$this->assign('datainfo',$datainfo);
    	$this->assign('datafields',$datafields);
    	$this->assign('data_list',$data_list);
    	//$this->assign('module_info',$module_info);
		$this->display();
	}
	/**
	 * 增加数据表名称
	 * 传参方式 post
	 * @author mss
	 * @time 2017-08-02
	 */
	public function addDatasheet(){
		if(session('M_TYPE') == 2){
			$this->error('您没有添加权限');
			//客户想添加模块
			/*if($_POST['p_id'] == 22){
				$this->error('您没有添加权限');
			}*/
		}
		$data['d_name'] = $_POST['mo_name'];
		$data['d_zh_name'] = $_POST['mo_e_name'];
		$id = $this->Datasheet->addDatasheet($data);
		if($id){
			$moname = $_POST['mo_name'];
			$mo_e_name = $_POST['mo_e_name'];
			$this->ajaxReturn(array('mo_name'=>$moname,'mo_e_name'=>$mo_e_name,'d_id'=>$id),'JSON');
		}
	}
	/**
	 * 增加字段
	 * 传参方式 post
	 * @author mss
	 * @time 2017-08-02
	 */
	public function addDatafield(){
		if(session('M_TYPE') == 2){
			$this->error('您没有添加权限');
		}
		$d_id = $_POST['mo_id'];
		$where0['d_id'] = $d_id;
		$data['description'] = $_POST['description'];
		$this->Datasheet->editDatasheet($where0,$data);
		$count = count($_POST['field_name']);
		$data = array();
		for($i=0;$i<$count;$i++){
			$data[$i]['d_id'] = $d_id;
			$where['field_name'] = $_POST['field_name'][$i];
			$data[$i]['field_name'] = $_POST['field_name'][$i];
			$data[$i]['field_type'] = $_POST['field_type'][$i];
			$data[$i]['default_val'] = $_POST['default_val'][$i];
			$data[$i]['note'] = $_POST['note'][$i];
		}
		$id = $this->Datafield->addAllFields($data);
		if($id){
			$this->success('添加成功');
		}else{
			$this->error('添加失败');
		}
	}


	/**
	 * 修改表说明
	 * 传参方式 post
	 * @author mss
	 * @time 2017-08-03
	 */
	public function editDatasheet(){
        if(session('M_TYPE') == 2){
            //客户想删除方法
			$data['info'] = '您没有编辑权限';
			$this->ajaxReturn($data);
        }
		$where['d_id'] = $_POST['fid'];
		$fnames = $_POST['fnames'];
		$data['description'] = $_POST['vals'];
		$res = $this->Datasheet->editDatasheet($where,$data);
		if($res){
			$this->success('修改成功');
		}else{
			$this->success('修改失败');
		}
	}
	
	/**
	 * 修改所选方法下的参数
	 */
	public function editPa(){
        if(session('M_TYPE') == 2){
            //客户想修改方法参数
                $this->error('您没有修改权限');
        }

		$where['d_id'] = $_POST['d_id'];
		$this->Datafield->deleteDatafield($where);
		$arr[] = array();
		$count = count($_POST['field_name']);
		for($i=0;$i<$count;$i++){
			$data[$i]['d_id'] = $_POST['d_id'];
			$data[$i]['field_name'] = $_POST['field_name'][$i];
			$data[$i]['field_type'] = $_POST['field_type'][$i];
			$data[$i]['default_val'] = $_POST['default_val'][$i];
			$data[$i]['note'] = $_POST['note'][$i];
		}
		$res = $this->Datafield->addAllFields($data);
		if($res){
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
	}

	/**
	 * 删除表
	 * 传参方式 post
	 * @author mss
	 * @time 2017-08-02
	 * @param d_id 数据表的id
	 */
	public function deleteDatasheet(){
		if(session('M_TYPE') == 2){
			//客户想删除表
			$this->error('您没有删除权限');
		}
		$where['d_id'] = $_POST['mo_id'];
		$id = $this->Datasheet->deleteDatasheet($where);
		if($id){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

}

?>